/**
 * Author: DingBaoyong
 * Desc: 天地图js
 */
$(function($) {

    let hg = new Ding();
    hg.Map.initMap(Ding.Map.MapType.TMAP, "container", {
        lng : 121.28869,
        lat : 31.16589,
        zoom : 12
    });
    
    top.chooseYhd = function(yhdId){
        hg.Map.moveToYhd(yhdId);
    }

    //获取所有隐患点数据
    $.get("hiddenPoint/queryAll.html", null, null, "json").done(function(data) {
        hg.Map.showYhdData(data);

        setInterval(function(){
            $.get("alarmStatistics/hiddenPoint.html", null, null, "json").done(function(data) {
                hg.Map.showAlarm(data);
            })
        },1000*10);
    });
});

(function(Global) {
    Global.Ding = function() {
    };

    Global.Ding.prototype.Util = {
        getLocation : function(successHandler, errorhandler) {
            let geolocation = window.navigator.geolocation
            if (geolocation) {
                let options = {
                    enableHighAccuracy : true,
                };
                geolocation.getCurrentPosition(successHandler, errorhandler,
                        options);
            } else {
                errorhandler && errorhandler();
            }
        }
    };

    Global.Ding.Map = {
        MapType : {
            TMAP : 0,
            BMAP : 1
        },
        TMAP : function() {
            let map;
            let markers=[], labels=[], controls=[], projectInfos=[];
            function addMapTypeSelector(container) {
                let html = [];
                html.push("<select id=\"mapTypeSelect\">");
                html.push("<option value=\"TMAP_NORMAL_MAP\">地图</option>");
                html.push("<option value=\"TMAP_SATELLITE_MAP\">卫星</option>");
                html.push("<option value=\"TMAP_HYBRID_MAP\">卫星混合</option>");
                html.push("<option value=\"TMAP_TERRAIN_MAP\">地形</option>");
                html.push("<option value=\"TMAP_TERRAIN_HYBRID_MAP\">地形混合</option>");
                html.push("</select>");
                let dom = document.createElement("div");
                dom.innerHTML = html.join(" ");
                if (typeof container == "string")
                    container = $("#" + container);
                container[0].appendChild(dom);
                setMapTypeSelectorEvent($(dom));
                return dom;
            }
            
            function setMapTypeSelectorEvent(selector) {
                return selector.on("change", function(obj) {
                    map.setMapType(eval(obj.target.value))
                });
            }
            
            function createMap(container, options) {
                map = new TMap(container, {
                    projection: "EPSG:4326"
                });
                map.centerAndZoom(new TLngLat(options.lng, options.lat),
                        options.zoom);
                map.enableDoubleClickZoom();
                map.enableInertia();
                map.enableHandleMouseScroll();

                map.setBounds(new TBounds(72.38009, 4.55467, 140.49532,
                        52.95534));
                map.setZoomLevels([ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
                        16, 17, 18 ]);
                //设置卫星混合地图
                map.setMapType(TMAP_HYBRID_MAP);
                setMapEvent("zoomend",function(zoom){
                    $.each(labels, function(index, label, o){
                        if(zoom < 6){
                            label.hide();
                        }else{
                            label.show();
                        }
                    });
                });
            }
            
            function setMapEvent(event, callback){
                TEvent.addListener(map, event, callback);
            }

            function setNavigationControl() {
                addControl(new TNavigationControl({
                    type : "TMAP_NAVIGATION_CONTROL_LARGE", // 缩放平移控件的显示类型
                    anchor : "TMAP_ANCHOR_TOP_LEFT" // 缩放平移控件显示的位置
                }));
            }

            function setMapTypeSelector(container) {
//                let mapTypeStyle = addMapTypeSelector(container);
//
//                let mapTyleControl = new THtmlElementControl(mapTypeStyle);
//                mapTyleControl.setRight(10);
//                mapTyleControl.setTop(10);
                let mapTypeControl = new TMapTypeControl();
                addControl(mapTypeControl);
            }
            
            function addControl(control){
                map.addControl(control);
                controls.push(control);
            }
            
            function addOverLay(overLay){
                map.addOverLay(overLay);
            }
            function addLabel(label){
                map.addOverLay(label);
                labels.push(label);
            }
            
            function setMapControls(container) {
                setNavigationControl();
                setMapTypeSelector(container);
            }
            
            function showAlarm(id, alarm){
                let idx,marker;
                for(let idx in markers){
                    marker = marker[idx];
                    if(marker.projectId == id){
                        marker.setIconImage(alarm?"./images/warning48.png":"./images/point16.png")
                        break;
                    }
                }
            }
            
            return {
                init: function(container, options) {
                    createMap(container, options);
                    //setMapControls(container);
                },
                createMaker: function(id, yhdName, yhdInfo, lng, lat, callbacks){
                    let marker = new TMarker(this.createLngLat(lng, lat));
                    marker.yhdName = yhdName;
                    marker.setTitle(yhdInfo);
                    marker.setIcon(this.createMarkerGetGreenIcon());
                    marker.yhdId = id;
                    callbacks = callbacks || {};
                    $.each(callbacks, function(event, callback){
                        TEvent.addListener(marker, event, callback);
                    });
                    markers.push(marker)
                    return marker;
                },
                createLngLat:function(long,lati){
                    return new TLngLat(long,lati);
                },
                createMarkerClusterer:function(config){
                    return new TMarkerClusterer(map,config);
                },
                createMarkerGetGreenIcon: function(){
                    return new TIcon("./images/point16.png", new TSize(37,33));
                },
                createMarkerGetYellowIcon:function(){
                    return new TIcon("https://map.tianditu.com/images/maptool/point15.png",new TSize(37,33));
                },
                createMarkerGetRedIcon:function(){
                    return new TIcon("https://map.tianditu.com/images/maptool/point14.png",new TSize(37,33));
                },
                addLableControl: function(text, lng, lat){
                    let position = this.createLngLat(lng, lat);
                    let label = new TLabel({text: text, position: position});
                    addLabel(label);
                    return label;
                },
                initialized:function(){
                    this.createMarkerClusterer({markers:markers});
                    if(markers.length>0){
                        map.panTo(markers[0].getLngLat(),14);
                    }
                },
                moveToYhd:function(yhdId, zoom){
                    $.each(markers, function(index, marker){
                        if(marker.yhdId == yhdId){
                            map.panTo(marker.getLngLat(), zoom || 14);
                        }
                    })
                },
                showAlarm: function(data){
                    let mi, marker, yi, yhdAlarm;
                    for(mi in markers){
                        marker = markers[mi];
                        marker.setIconImage("./images/point16.png");
                        for(yi in data){
                            yhdAlarm = data[yi]
                            if(marker.yhdId == yhdAlarm.yhdId){
                                if(yhdAlarm.count){
                                    marker.setIconImage("./images/warning48.png");
                                    break;
                                }
                            }
                        }
                    }
                },
                addYhdInfo:function(data){
                    let yhdType = getValueByIndex("yhdType", data.yhdType);
                    let jcStage = getValueByIndex("jcStage", data.jcStage);
                    let monitorLevel = getValueByIndex("monitorLevel", data.monitorLevel);
                    let yf = getValueByIndex("yf", data.yf);
                    let wxdx = getValueByIndex("wxdx", data.wxdx);
                    let yhdInfo = "隐患点名称: " + data.yhdName + "\n" +
                        "隐患点类型: " + yhdType + "\n" +
                        "监测阶段: " + jcStage + "\n" +
                        "监测级别: " + monitorLevel + "\n" +
                        "诱发因素: " + yf + "\n" +
                        "威胁对象: " + wxdx + "\n";
                    return yhdInfo;
                }
            };
        }
    };

    Global.Ding.prototype.Map = {

        setMapType : function(mapType) {
            this.mapType = mapType;
        },
        setMap : function(map) {
            this.map = map;
        },
        getMap : function() {
            return this.map;
        },
        initMap : function(mapType, container, options) {
            let _this = this;
            let args = arguments;
            this.mapType = mapType;
            if (typeof container == "object") {
                options = container;
                container = undefined;
            }
            container = container || "map";
            options = options || {};
            if (_this.mapType === Ding.Map.MapType.TMAP) {
                _this.mapHandler = new Ding.Map.TMAP();
                _this.mapHandler.init(container, options);
            }
        },
        showYhdData: function (data){
            let _this = this, marker, mapHandler = _this.mapHandler;
            let yhdInfo;
            $.each(data, function(index, item){
                yhdInfo = mapHandler.addYhdInfo(item);
                marker = mapHandler.createMaker(item.id, item.yhdName, yhdInfo, item.lng, item.lat,{"click": function(){
                    //top.navtab.addTabItem({tabid:"bg_"+data.projectId,text:data.projectName,url:"./project/bg.html?projectId="+data.projectId});
                    showBgModal(item.id);
                }});
                mapHandler.addLableControl(item.yhdName, item.lng, item.lat);
            });
            mapHandler.initialized();
        },
        moveToYhd: function(yhdId, zoom){
            let _this = this, mapHandler = _this.mapHandler;
            mapHandler.moveToYhd(yhdId, zoom);
        },
        showAlarm: function(data){
            let _this = this, mapHandler = _this.mapHandler;
            mapHandler.showAlarm(data);
        }
    };

})(this);
